import turtle
from turtle_utils import ignore_terminator_exception

"""
用 python 的 turtle 模块创建一个函数，功能是绘画一个梦幻螺旋，
函数传入的参数为绘画速度，默认最快。代码的注释请用中文，但是函数和参数用英文。
"""


@ignore_terminator_exception
def olympic_rings(speed=10):
    """
    用 turtle 模块绘画奥运五环
    代码来源：https://blog.csdn.net/qq_63692227/article/details/131507160
    """
    turtle.TurtleScreen._RUNNING = True  # 解决 turtle.Terminator 异常的问题
    # 设置公共属性
    turtle.width(10)

    turtle.speed(speed)

    # 第一个环
    turtle.color("black")
    turtle.circle(50)

    # 第二个环
    turtle.penup()
    turtle.goto(-120, 0)
    turtle.pendown()
    turtle.color("blue")
    turtle.circle(50)

    # 第三个环
    turtle.penup()
    turtle.goto(120, 0)
    turtle.pendown()
    turtle.color("red")
    turtle.circle(50)

    # 第四个环
    turtle.penup()
    turtle.goto(-60, -50)
    turtle.pendown()
    turtle.color("yellow")
    turtle.circle(50)

    # 第五个环
    turtle.penup()
    turtle.goto(60, -50)
    turtle.pendown()
    turtle.color("green")
    turtle.circle(50)
    # turtle.exitonclick()
    turtle.done()


@ignore_terminator_exception
def tree(speed=10):
    """
    用 turtle 模块绘画树
    代码来源：https://blog.csdn.net/hhy321/article/details/131142412
    """
    toplevel = 3  # 一共递归8层
    angle = 10
    rangle = 15

    turtle.TurtleScreen._RUNNING = True  # 解决 turtle.Terminator 异常的问题

    # 设置画笔移动速度，画笔绘制的速度范围[0,10]整数，数字越大越快。
    # 而turtle.speed(0)是没有延迟，但是代码多的图案直接显示图案是不行的。
    turtle.speed(speed)

    def drawTree(length, level):
        turtle.left(angle)  # 绘制左枝
        turtle.color("black")
        turtle.forward(length)

        if level == toplevel:  # 叶子
            turtle.color("pink")
            turtle.circle(2, 360)

        if level < toplevel:  # 在左枝退回去之前递归
            drawTree(length - 10, level + 1)
        turtle.back(length)

        turtle.right(angle + rangle)  # 绘制右枝
        turtle.color("black")
        turtle.forward(length)

        if level == toplevel:  # 叶子
            turtle.color("pink")
            turtle.circle(2, 360)

        if level < toplevel:  # 在右枝退回去之前递归
            drawTree(length - 10, level + 1)
            turtle.color("black")
        turtle.back(length)
        turtle.left(rangle)

    def writePoem():
        turtle.color("blue")
        turtle.penup()
        turtle.goto(150, -120)
        turtle.write('碧玉妆成一树高，', font=("隶书", 15))
        turtle.goto(150, -140)
        turtle.write('万条垂下绿丝绦。', font=("隶书", 15))
        turtle.goto(150, -160)
        turtle.write('不知细叶谁裁出，', font=("隶书", 15))
        turtle.goto(150, -180)
        turtle.write('二月春风似剪刀。', font=("隶书", 15))
        turtle.hideturtle()

    turtle.left(90)
    turtle.penup()
    turtle.back(300)
    turtle.pendown()
    turtle.forward(100)
    # turtle.speed('fastest')
    turtle.speed(speed)
    # drawTree(80, 1)
    drawTree(80, 1)
    writePoem()
    # turtle.exitonclick()
    turtle.done()


@ignore_terminator_exception
def doraemon(speed=10):
    """
    用 turtle 模块绘画树
    代码来源：https://blog.csdn.net/weixin_57825040/article/details/127356134
    """

    turtle.TurtleScreen._RUNNING = True  # 解决 turtle.Terminator 异常的问题
    turtle.speed(speed)

    # turtle.bye()
    def flyTo(x, y):  # 开启无轨迹跳跃
        turtle.penup()
        turtle.goto(x, y)
        turtle.pendown()

    def drawEye():  # 画眼睛
        turtle.tracer(False)
        a = 2.5
        for i in range(120):
            if 0 <= i < 30 or 60 <= i < 90:
                a -= 0.05
            else:
                a += 0.05

            turtle.left(3)
            turtle.fd(a)
        turtle.tracer(True)

    def beard():  # 画胡子
        # 左边第一根胡子
        flyTo(-37, 135)
        turtle.seth(165)
        turtle.fd(60)

        # 左边第二根胡子
        flyTo(-37, 125)
        turtle.seth(180)
        turtle.fd(60)

        # 左边第三根胡子
        flyTo(-37, 115)
        turtle.seth(193)
        turtle.fd(60)

        # 右边第一根胡子
        flyTo(37, 135)
        turtle.seth(15)
        turtle.fd(60)

        # 右边第二根胡子
        flyTo(37, 125)
        turtle.seth(0)
        turtle.fd(60)

        # 右边第三根胡子
        flyTo(37, 115)
        turtle.seth(-13)
        turtle.fd(60)

    def drawRedScarf():
        turtle.fillcolor("red")  # 填充颜色
        turtle.begin_fill()
        turtle.seth(0)  # 调整转向向右
        turtle.fd(200)
        turtle.circle(-5, 90)
        turtle.fd(10)
        turtle.circle(-5, 90)
        turtle.fd(207)
        turtle.circle(-5, 90)
        turtle.fd(10)
        turtle.circle(-5, 90)
        turtle.end_fill()

    def drawMouse():
        flyTo(5, 148)
        turtle.seth(270)
        turtle.fd(100)
        turtle.seth(0)
        turtle.circle(120, 50)
        turtle.seth(230)
        turtle.circle(-120, 100)

    def drawRedNose():
        flyTo(-10, 158)
        turtle.fillcolor("red")  # 填充颜色
        turtle.begin_fill()
        turtle.circle(20)
        turtle.end_fill()

    def drawBlackdrawEye():
        turtle.seth(0)
        flyTo(-20, 195)
        turtle.fillcolor("#000000")  # 填充颜色
        turtle.begin_fill()
        turtle.circle(13)
        turtle.end_fill()
        turtle.pensize(6)
        flyTo(20, 205)
        turtle.seth(75)
        turtle.circle(-10, 150)
        turtle.pensize(3)
        flyTo(-17, 200)
        turtle.seth(0)
        turtle.fillcolor("#ffffff")
        turtle.begin_fill()
        turtle.circle(5)
        turtle.end_fill()
        flyTo(0, 0)

    def drawFace():
        turtle.forward(183)  # 前行183个单位
        turtle.fillcolor("white")  # 填充颜色为白色
        turtle.begin_fill()  # 开始填充
        turtle.left(45)  # 左转45度
        turtle.circle(120, 100)  # 右边那半边脸
        turtle.seth(90)  # 调整转向向上
        drawEye()  # 画右眼睛
        turtle.seth(180)  # 调整转向向左
        turtle.penup()  # 抬笔
        turtle.fd(60)  # 前行60
        turtle.pendown()  # 落笔
        turtle.seth(90)  # 调整转向向上
        drawEye()  # 画左眼睛
        turtle.penup()  # 抬笔
        turtle.seth(180)  # 调整转向向左
        turtle.fd(64)  # 前进64
        turtle.pendown()  # 落笔
        turtle.seth(215)  # 修改朝向
        turtle.circle(120, 100)  # 左边那半边脸
        turtle.end_fill()  #

    def drawHead():
        turtle.penup()  # 抬笔
        turtle.circle(150, 40)  # 画圆, 半径150，圆周角40
        turtle.pendown()  # 落笔
        turtle.fillcolor("#00a0de")  # 填充色
        turtle.begin_fill()  # 开始填充
        turtle.circle(150, 280)  # 画圆，半径150, 圆周角280
        turtle.end_fill()

    def drawAll():
        drawHead()
        drawRedScarf()
        drawFace()
        drawRedNose()
        drawMouse()
        beard()
        flyTo(0, 0)
        turtle.seth(0)
        turtle.penup()
        turtle.circle(150, 50)
        turtle.pendown()
        turtle.seth(30)
        turtle.fd(40)
        turtle.seth(70)
        turtle.circle(-30, 270)
        turtle.fillcolor("#00a0de")
        turtle.begin_fill()
        turtle.seth(230)
        turtle.fd(80)
        turtle.seth(90)
        turtle.circle(1000, 1)
        turtle.seth(-89)
        turtle.circle(-1000, 10)
        turtle.seth(180)
        turtle.fd(70)
        turtle.seth(90)
        turtle.circle(30, 180)
        turtle.seth(180)
        turtle.fd(70)
        turtle.seth(100)
        turtle.circle(-1000, 9)
        turtle.seth(-86)
        turtle.circle(1000, 2)
        turtle.seth(230)
        turtle.fd(40)
        turtle.circle(-30, 230)
        turtle.seth(45)
        turtle.fd(81)
        turtle.seth(0)
        turtle.fd(203)
        turtle.circle(5, 90)
        turtle.fd(10)
        turtle.circle(5, 90)
        turtle.fd(7)
        turtle.seth(40)
        turtle.circle(150, 10)
        turtle.seth(30)
        turtle.fd(40)
        turtle.end_fill()

        # 左手
        turtle.seth(70)
        turtle.fillcolor("#FFFFFF")
        turtle.begin_fill()
        turtle.circle(-30)
        turtle.end_fill()

        # 脚
        flyTo(103.74, -182.59)
        turtle.seth(0)
        turtle.fillcolor("#FFFFFF")
        turtle.begin_fill()
        turtle.fd(15)
        turtle.circle(-15, 180)
        turtle.fd(90)
        turtle.circle(-15, 180)
        turtle.fd(10)
        turtle.end_fill()
        flyTo(-96.26, -182.59)
        turtle.seth(180)
        turtle.fillcolor("#FFFFFF")
        turtle.begin_fill()
        turtle.fd(15)
        turtle.circle(15, 180)
        turtle.fd(90)
        turtle.circle(15, 180)
        turtle.fd(10)
        turtle.end_fill()

        # 右手
        flyTo(-133.97, -91.81)
        turtle.seth(50)
        turtle.fillcolor("#FFFFFF")
        turtle.begin_fill()
        turtle.circle(30)
        turtle.end_fill()

        # 口袋
        flyTo(-103.42, 15.09)
        turtle.seth(0)
        turtle.fd(38)
        turtle.seth(230)
        turtle.begin_fill()
        turtle.circle(90, 260)
        turtle.end_fill()
        flyTo(5, -40)
        turtle.seth(0)
        turtle.fd(70)
        turtle.seth(-90)
        turtle.circle(-70, 180)
        turtle.seth(0)
        turtle.fd(70)

        # 铃铛
        flyTo(-103.42, 15.09)
        turtle.fd(90)
        turtle.seth(70)
        turtle.fillcolor("#ffd200")
        turtle.begin_fill()
        turtle.circle(-20)
        turtle.end_fill()
        turtle.seth(170)
        turtle.fillcolor("#ffd200")
        turtle.begin_fill()
        turtle.circle(-2, 180)
        turtle.seth(10)
        turtle.circle(-100, 22)
        turtle.circle(-2, 180)
        turtle.seth(180 - 10)
        turtle.circle(100, 22)
        turtle.end_fill()
        flyTo(-13.42, 15.09)
        turtle.seth(250)
        turtle.circle(20, 110)
        turtle.seth(90)
        turtle.fd(15)
        turtle.dot(10)
        flyTo(0, -150)
        drawBlackdrawEye()

    turtle.screensize(800, 6000, "#F0F0F0")
    turtle.pensize(3)
    turtle.speed(9)
    drawAll()
    turtle.penup()  # 抬笔
    turtle.goto(100, -300)
    # turtle.color("violet")     #粉红色有点那个
    turtle.write('by peak', font=("Bradley Hand ITC", 30, "bold"))
    # turtle.exitonclick()
    turtle.done()


@ignore_terminator_exception
def party_flag(speed=10):
    """
    用 turtle 模块绘制党旗
    代码来源：https://blog.csdn.net/weixin_57825040/article/details/127356134
    """
    turtle.TurtleScreen._RUNNING = True  # 解决 turtle.Terminator 异常的问题
    turtle.speed(speed)
    turtle.fillcolor("red")
    turtle.pencolor("red")
    turtle.up()
    turtle.goto(-300, 300)
    turtle.down()
    turtle.begin_fill()
    for i in range(2):
        turtle.forward(600)
        turtle.right(90)
        turtle.forward(400)
        turtle.right(90)
    turtle.end_fill()

    turtle.fillcolor("yellow")
    turtle.pencolor("yellow")

    turtle.up()
    turtle.goto(10, 220)
    turtle.seth(225)
    turtle.down()
    turtle.begin_fill()
    turtle.fd(125)
    turtle.right(90)
    turtle.fd(50)
    turtle.right(90)
    turtle.fd(100)
    turtle.right(90)
    turtle.circle(25, 90)
    turtle.end_fill()
    turtle.up()
    turtle.goto(-40, 190)
    turtle.seth(-45)
    turtle.down()
    turtle.begin_fill()
    for i in range(2):
        turtle.forward(200)
        turtle.right(90)
        turtle.forward(30)
        turtle.right(90)
    turtle.end_fill()

    turtle.up()
    turtle.goto(-100, 100)
    turtle.seth(-50)
    turtle.down()
    turtle.begin_fill()
    turtle.circle(100, 180)
    turtle.fd(20)
    turtle.right(157)
    turtle.circle(-115, 190)
    turtle.left(90)
    turtle.fd(20)
    turtle.right(90)
    turtle.fd(20)
    turtle.right(90)
    turtle.fd(20)
    turtle.left(80)
    turtle.fd(30)
    turtle.end_fill()
    turtle.up()
    turtle.goto(-90, 50)
    turtle.down()
    turtle.begin_fill()
    turtle.circle(20)
    turtle.end_fill()
    turtle.hideturtle()  # 隐藏小海龟
    # turtle.exitonclick()
    # 维持面板
    turtle.done()
    # turtle.mainloop()


@ignore_terminator_exception
def draw_mystical_spiral(speed=10):
    """
    魔鬼螺旋¶
    代码来源：https://blog.csdn.net/weixin_57825040/article/details/127356134
    """
    import turtle as t
    from turtle import up, color, goto, down
    turtle.TurtleScreen._RUNNING = True  # 解决 turtle.Terminator 异常的问题
    turtle.speed(speed)

    angle = 60  # 通过改变角度，绘制出各种多边形
    t.setup(1280, 720)
    t.bgcolor('black')
    t.pensize(2)
    randomColor = ['red', 'blue', 'green', 'purple', 'gold', 'pink']
    t.speed(0)
    for i in range(600):
        t.color(randomColor[i % 6])
        t.fd(i)
        t.rt(angle + 1)
    up()
    color("#0fe6ca")
    goto(0, 0)
    down()
    t.done()


@ignore_terminator_exception
def draw_mystical_spiral2(speed=10):
    """
    创建绘制梦幻螺旋的函数
    """
    import random
    turtle.TurtleScreen._RUNNING = True  # 解决 turtle.Terminator 异常的问题
    # 设置绘画速度
    turtle.speed(speed)

    # 绘制梦幻螺旋
    for i in range(360):
        # 随机选择颜色
        r = random.random()
        g = random.random()
        b = random.random()
        turtle.color(r, g, b)

        turtle.forward(i)
        turtle.left(59)

    # 隐藏画笔
    turtle.hideturtle()

    # 显示结果
    turtle.done()


if __name__ == '__main__':
    # olympic_rings()  # 绘制奥运五环
    # tree() # 绘制树
    # doraemon()  # 绘制哆啦A梦
    # party_flag()  # 绘制党旗

    # 调用函数绘制梦幻螺旋
    draw_mystical_spiral()
